#ifndef IMAGEPROVIDER_H
#define IMAGEPROVIDER_H

#include <QQuickImageProvider>
#include <QMap>
#include <QImage>
#include <QString>
#include<QWindow>
#include<iostream>
#include"Backend.h"
class ImageProvider : public QQuickImageProvider {
    Q_OBJECT
public:
    ImageProvider() : QQuickImageProvider(QQuickImageProvider::Image) {}

    QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override {
        if (backend!=nullptr&&id.contains("target")&&backend->targetImageCache.contains(id)) {
            return QImage(backend->targetImageCache[id]);
        }
        else if (backend!=nullptr&&id.contains("template")&&backend->templateImageCache.contains(id)) {
            return QImage(backend->templateImageCache[id]);
        }
        return QImage();
    }
    QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override {
        QImage image;
        if (backend!=nullptr&&id.contains("target")&&backend->targetImageCache.contains(id)) {
            image=QImage(backend->targetImageCache[id]);
        }
        else if (backend!=nullptr&&id.contains("template")&&backend->templateImageCache.contains(id)) {
            image=QImage(backend->templateImageCache[id]);
        }
        return QPixmap::fromImage(image.scaled(requestedSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
    static void setBackend(Backend *b) { backend = b; }

private:
    static Backend *backend;
};

#endif // IMAGEPROVIDER_H
